home *** CD-ROM | disk | FTP | other *** search
/ The CICA Windows Explosion! / The CICA Windows Explosion! - Disc 2.iso / programr / ole2book.zip / CHAP06.ZIP / CHAP06 / EDATAOBJ / RENDER.CPP < prev   
C/C++ Source or Header  |  1993-05-21  |  6KB  |  276 lines

  1. /*
  2.  * RENDER.CPP
  3.  * Data Object for Chapter 6
  4.  *
  5.  * CDataObject::Render* functions to create text, bitmaps, and metafiles
  6.  * in a variety of sizes.
  7.  *
  8.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  9.  *
  10.  * Kraig Brockschmidt, Software Design Engineer
  11.  * Microsoft Systems Developer Relations
  12.  *
  13.  * Internet  :  kraigb@microsoft.com
  14.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  15.  */
  16.  
  17.  
  18. #include "dataobj.h"
  19. #include <string.h>
  20.  
  21.  
  22.  
  23. /*
  24.  * CDataObject::RenderText
  25.  *
  26.  * Purpose:
  27.  *  Creates a global memory block containing the letter 'k' of sizes
  28.  *  of 64 bytes, 1024 bytes, and 16384 bytes, into a caller-supplied
  29.  *  STGMEDIUM.
  30.  *
  31.  * Parameters:
  32.  *  pSTM            LPSTGMEDIUM in which to render.
  33.  *
  34.  * Return Value:
  35.  *  HRESULT         Return value for ::GetData
  36.  */
  37.  
  38. HRESULT CDataObject::RenderText(LPSTGMEDIUM pSTM)
  39.     {
  40.     DWORD       cch;
  41.     HGLOBAL     hMem;
  42.     LPSTR       psz;
  43.     UINT        i;
  44.  
  45.     //Get the size of data we're dealing with
  46.     switch (m_iSize)
  47.         {
  48.         case DOSIZE_SMALL:
  49.             cch=CCHTEXTSMALL;
  50.             break;
  51.  
  52.         case DOSIZE_MEDIUM:
  53.             cch=CCHTEXTMEDIUM;
  54.             break;
  55.  
  56.         case DOSIZE_LARGE:
  57.             cch=CCHTEXTLARGE;
  58.             break;
  59.  
  60.         default:
  61.             return ResultFromScode(E_FAIL);
  62.         }
  63.  
  64.     hMem=GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE, cch);
  65.  
  66.     if (NULL==hMem)
  67.         return ResultFromScode(STG_E_MEDIUMFULL);
  68.  
  69.     psz=(LPSTR)GlobalLock(hMem);
  70.  
  71.     for (i=0; i < cch-1; i++)
  72.         *(psz+i)=' ' + (i % 32);
  73.  
  74.     *(psz+i)=0;
  75.  
  76.     GlobalUnlock(hMem);
  77.  
  78.     pSTM->hGlobal=hMem;
  79.     pSTM->tymed=TYMED_HGLOBAL;
  80.     return NOERROR;
  81.     }
  82.  
  83.  
  84.  
  85.  
  86. /*
  87.  * CDataObject::RenderBitmap
  88.  *
  89.  * Purpose:
  90.  *  Creates a new bitmap into which we copy a bitmap loaded
  91.  *  from our resources.
  92.  *
  93.  * Parameters:
  94.  *  pSTM            LPSTGMEDIUM in which to render.
  95.  *
  96.  * Return Value:
  97.  *  HRESULT         Return value for ::GetData
  98.  */
  99.  
  100. HRESULT CDataObject::RenderBitmap(LPSTGMEDIUM pSTM)
  101.     {
  102.     HBITMAP     hBmp;
  103.     UINT        cxy;
  104.     HDC         hDC, hDCSrc, hDCDst;
  105.  
  106.     //Get the size of bitmap we're dealing with
  107.     switch (m_iSize)
  108.         {
  109.         case DOSIZE_SMALL:
  110.             cxy=CXYBITMAPSMALL;
  111.             break;
  112.  
  113.         case DOSIZE_MEDIUM:
  114.             cxy=CXYBITMAPMEDIUM;
  115.             break;
  116.  
  117.         case DOSIZE_LARGE:
  118.             cxy=CXYBITMAPLARGE;
  119.             break;
  120.  
  121.         default:
  122.             return ResultFromScode(E_FAIL);
  123.         }
  124.  
  125.     //Get two memory DCs between which to BitBlt.
  126.     hDC=GetDC(NULL);
  127.     hDCSrc=CreateCompatibleDC(hDC);
  128.     hDCDst=CreateCompatibleDC(hDC);
  129.     ReleaseDC(NULL, hDC);
  130.  
  131.     if (NULL==hDCSrc || NULL==hDCDst)
  132.         {
  133.         if (NULL!=hDCDst)
  134.             DeleteDC(hDCDst);
  135.  
  136.         if (NULL!=hDCSrc)
  137.             DeleteDC(hDCSrc);
  138.  
  139.         return ResultFromScode(STG_E_MEDIUMFULL);
  140.         }
  141.  
  142.     SelectObject(hDCSrc, m_rghBmp[m_iSize-DOSIZE_SMALL]);
  143.  
  144.     hBmp=CreateCompatibleBitmap(hDCSrc, cxy, cxy);
  145.  
  146.     if (NULL==hBmp)
  147.         {
  148.         DeleteDC(hDCDst);
  149.         DeleteDC(hDCSrc);
  150.  
  151.         if (NULL!=hBmp)
  152.             DeleteObject(hBmp);
  153.  
  154.         return ResultFromScode(STG_E_MEDIUMFULL);
  155.         }
  156.  
  157.     //Copy from the source to destination
  158.     SelectObject(hDCDst, hBmp);
  159.     BitBlt(hDCDst, 0, 0, cxy, cxy, hDCSrc, 0, 0, SRCCOPY);
  160.  
  161.     DeleteDC(hDCDst);
  162.     DeleteDC(hDCSrc);
  163.  
  164.     pSTM->hGlobal=(HGLOBAL)hBmp;
  165.     pSTM->tymed=TYMED_GDI;
  166.     return NOERROR;
  167.     }
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174. /*
  175.  * CDataObject::RenderMetafilePict
  176.  *
  177.  * Purpose:
  178.  *  Creates a metafile containing blue shaded bands.
  179.  *
  180.  * Parameters:
  181.  *  pSTM            LPSTGMEDIUM in which to render.
  182.  *
  183.  * Return Value:
  184.  *  HRESULT         Return value for ::GetData
  185.  */
  186.  
  187. HRESULT CDataObject::RenderMetafilePict(LPSTGMEDIUM pSTM)
  188.     {
  189.     HDC             hDC;
  190.     HGLOBAL         hMem;
  191.     HMETAFILE       hMF;
  192.     LPMETAFILEPICT  pMF;
  193.     HBRUSH          hBrush;
  194.     HGDIOBJ         hBrT;
  195.     UINT            cRec;
  196.     int             x, y, dxy;
  197.     RECT            rc;
  198.  
  199.     switch (m_iSize)
  200.         {
  201.         case DOSIZE_SMALL:
  202.             cRec=CRECMETAFILESMALL;
  203.             break;
  204.  
  205.         case DOSIZE_MEDIUM:
  206.             cRec=CRECMETAFILEMEDIUM;
  207.             break;
  208.  
  209.         case DOSIZE_LARGE:
  210.             cRec=CRECMETAFILELARGE;
  211.             break;
  212.  
  213.         default:
  214.             return ResultFromScode(E_FAIL);
  215.         }
  216.  
  217.  
  218.     hDC=(HDC)CreateMetaFile(NULL);
  219.  
  220.     if (NULL!=hDC)
  221.         {
  222.         /*
  223.          * Draw something into the metafile.  For this object we
  224.          * draw some number of rectangles equal to the number of
  225.          * records we want.  So take the square root of the number
  226.          * of records and iterate over that number in both x & y.
  227.          */
  228.  
  229.         dxy=(int)1024/cRec;
  230.  
  231.  
  232.         //This creates a blue shading from light (top) to dark (bottom)
  233.         for (y=1024; y >=0; y-=dxy)
  234.             {
  235.             hBrush=CreateSolidBrush(RGB(0, 0, (1024-y)/4));
  236.             hBrT=SelectObject(hDC, (HGDIOBJ)hBrush);
  237.  
  238.             for (x=0; x < 1024; x+=dxy)
  239.                 {
  240.                 SetRect(&rc, x, y, x+dxy, y+dxy);
  241.                 FillRect(hDC, &rc, hBrush);
  242.                 }
  243.  
  244.             SelectObject(hDC, hBrT);
  245.             DeleteObject(hBrush);
  246.             }
  247.  
  248.         hMF=CloseMetaFile(hDC);
  249.         }
  250.  
  251.     if (NULL==hMF)
  252.         return ResultFromScode(STG_E_MEDIUMFULL);
  253.  
  254.     //Allocate the METAFILEPICT structure.
  255.     hMem=GlobalAlloc(GMEM_SHARE | GMEM_MOVEABLE, sizeof(METAFILEPICT));
  256.  
  257.     if (NULL==hMem)
  258.         {
  259.         DeleteMetaFile(hMF);
  260.         return ResultFromScode(STG_E_MEDIUMFULL);
  261.         }
  262.  
  263.     pMF=(LPMETAFILEPICT)GlobalLock(hMem);
  264.  
  265.     pMF->hMF=hMF;
  266.     pMF->mm=MM_ANISOTROPIC;
  267.     pMF->xExt=1024;
  268.     pMF->yExt=1024;
  269.  
  270.     GlobalUnlock(hMem);
  271.  
  272.     pSTM->hGlobal=hMem;
  273.     pSTM->tymed=TYMED_MFPICT;
  274.     return NOERROR;
  275.     }
  276.